package com.atguigu.gulimall.product.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.vo.AttrGroupRespVO;
import com.atguigu.gulimall.product.vo.AttrRespVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author LiQianJin
 * @email 18410584912@qq.com
 * @date 2024-03-30 14:43:23
 */
@RestController
@RequestMapping("product/attrgroup")
@Slf4j
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * @param params:分页请求参数
     * @param catelogId:三级分类ID
     * @Description:
     * @Author: LiQianJin
     * @Date: 2024/3/30 14:55
     * @return: com.atguigu.common.utils.R
     **/
    @GetMapping("/list/{catelogId}")
    public R queryAttrGroupDetailInfoPage(@RequestParam Map<String, Object> params,
                                          @PathVariable("catelogId") Long catelogId) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AttrGroupController.queryAttrGroupDetailInfo.params:{}", JSON.toJSONString(params));
            }
            PageUtils page = this.attrGroupService.queryAttrGroupDetailInfoPage(params, catelogId);
            return R.ok().put("page", page);
        } catch (Exception e) {
            log.error("AttrGroupController.queryAttrGroupDetailInfo.error:{}", e, e.getMessage());
            return R.error(e.getMessage());
        }

    }

    /***
     * @Description: 获取属性分组详情进行回显
     * @Author: LiQianJin
     * @Date: 2024/3/30 15:40
     * @param attrGroupId: 分组ID
     * @return: com.atguigu.common.utils.R
     **/
    @GetMapping("/info/{attrGroupId}")
    public R queryAttrGroupInfo(@PathVariable("attrGroupId") Long attrGroupId) {
        try {
            log.info("AttrGroupController.queryAttrGroupInfo.attrGroupId:{}", attrGroupId);
            AttrGroupRespVO respVO = this.attrGroupService.queryAttrGroupInfo(attrGroupId);
            return R.ok().put("data", respVO);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /***
     * @Description: 获取属性分组的关联的所有属性
     * @Author: LiQianJin
     * @Date: 2024/3/30 18:11
     * @param attrGroupId: 属性分组ID
     * @return: com.atguigu.common.utils.R
     **/
    @GetMapping("/{attrGroupId}/attr/relation")
    public R queryAttrGroupRelationInfo(@PathVariable("attrGroupId") Long attrGroupId) {
        try {
            log.info("AttrGroupController.queryAttrGroupRelationInfo.attrGroupId:{}", attrGroupId);
            List<AttrRespVO> respVOs = this.attrGroupService.queryAttrGroupRelationInfo(attrGroupId);
            return R.ok().put("data", respVOs);
        } catch (Exception e) {
            log.error("AttrGroupController.queryAttrGroupRelationInfo.error:{}", e, e.getMessage());
            return R.error(e.getMessage());
        }
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 批量删除
     */
    @PostMapping("/delete")
    public R batchRemove(@RequestBody List<Long> idList) {
        attrGroupService.batchRemove(idList);
        return R.ok();
    }

}
